package com.daxia.ogmall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.ogmall.daobase.GenericDAOHibernate;
import com.daxia.ogmall.dto.FactoryInfoDTO;
import com.daxia.ogmall.model.FactoryInfo;
import com.daxia.ogmall.support.Page;

@Repository
public class FactoryInfoDAO extends GenericDAOHibernate<FactoryInfo> {

    public List<FactoryInfo> find(FactoryInfoDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From FactoryInfo n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	}
        }			
        if (StringUtils.isNotBlank(dto.getEmail())) {
            hql.append(" and n.email = ? ");
            paras.add(dto.getEmail());
        }		
        if (StringUtils.isNotBlank(dto.getLegalPerson())) {
            hql.append(" and n.legalPerson like ? ");
            paras.add("%" + dto.getLegalPerson() + "%");
        }					
        if (dto.getStatus() != null) {
            hql.append(" and n.status = ? ");
            paras.add(dto.getStatus());
        }
        if (StringUtils.isNotBlank(dto.getExactName())) {
        	hql.append(" and n.user.username = ? ");
        	paras.add(dto.getExactName());
        }
        if (StringUtils.isNotBlank(dto.getSearchTerm())) {
        	hql.append(" and (n.user.username like ? or"
        			+ " n.user.nickName like ? "
        			+ " or n.user.realName like ?"
        			+ " or n.user.mobile like ?"
        			+ " or n.companyName like ? "
        			+ " or n.registerAddress like ? "
        			+ " or n.officeAddress like ? "
        			+ " or n.email like ? ) ");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        	paras.add("%" + dto.getSearchTerm() + "%");
        }
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public FactoryInfo findFactoryByUserId(Long userId) {
    	StringBuilder hql = new StringBuilder();
        Object[] paras = new Object[1];
    	hql.append("From FactoryInfo n where 1 = 1 and n.user.id = ? ");
    	paras[0] = userId;
    	return super.findOne(hql.toString(), paras);
    }
    
    public FactoryInfo findOne(FactoryInfoDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<FactoryInfo> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
    
    public int updateFactoryStatus(Long factoryId,Integer status) {
    	StringBuffer hql = new StringBuffer();
    	hql.append("update FactoryInfo set status = ? where id = ? ");
    	Object[] paras = new Object[]{status,factoryId};
    	return super.executeUpdate(hql.toString(), paras);
    }
   
}
